package cn.o.app.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;

@SuppressWarnings("unchecked")
public class OView extends RelativeLayout implements IView, IViewFinder {

	public OView(Context context) {
		super(context);
		onCreate(context, null);
	}

	public OView(Context context, AttributeSet attrs) {
		super(context, attrs);
		onCreate(context, attrs);
	}

	public OView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		onCreate(context, attrs);
	}

	protected void onCreate(Context context, AttributeSet attrs) {

	}

	public void setContentView(View view) {
		this.removeAllViews();
		this.addView(view);
	}

	public void setContentView(int layoutResID) {
		this.removeAllViews();
		LayoutInflater.from(getContext()).inflate(layoutResID, this);
	}

	@Override
	public <T extends View> T findViewById(int id, Class<T> viewClass) {
		View v = findViewById(id);
		if (v != null) {
			if (viewClass != null) {
				if (viewClass.isInstance(v)) {
					return (T) v;
				} else {
					return null;
				}
			}
			return (T) v;
		}
		return null;
	}

	@Override
	public View toView() {
		return this;
	}

}
